<?php

class OtsController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    private function deFormatDate($phpDate){
      return substr($phpDate,6,4)."-".substr($phpDate,3,2)."-".substr($phpDate,0,2);
    }

    public function indexAction()
    {
      $request=$this->getRequest();
      $conn=new Application_Model_Database();
      $sql="";
      if($request->getParam('op','')=="u" && $request->getParam('mag_id','')!="")
      {
        $sql="SELECT mag_folio,
          mag_year,
          mag_costo,
          approved,
          open,
          mag_comment,
          mag_currency,
          mag_iva,
          mag_pago,
          mag_cambio,
          insert_date
        FROM magove_t_folios
        WHERE mag_id='".$this->getRequest()->getParam('mag_id')."';";
      }
      else if($request->getParam('op','')=="" && $request->getParam('mag_id','')=="")
      {
        $sql="SELECT mag_cliente, mag_cdesc
          FROM magove_t_clientes;";
        $rs=$conn->doQuery($sql);
        $clients=$rs->fetchAll();
        $this->view->clients=$clients;
        
        $sql="SELECT mag_folio,
          mag_year,
          '' AS mag_costo,
          '' AS approved,
          '' AS open,
          '' AS mag_comment,
          'P' AS mag_currency,
          '0' AS mag_iva,
          '' AS mag_pago,
          '' AS mag_cambio,
          '' AS insert_date
          FROM magove_t_folios
          ORDER BY mag_year DESC, mag_folio DESC
          LIMIT 1;";
      }
      else
      {
        throw new Zend_Controller_Action_Exception('La información del folio está incompleta');
      }
      $rs=$conn->doQuery($sql);
      $row=$rs->fetch();
      if(!($request->getParam('op','')=="u" && $request->getParam('mag_id','')!=""))
      {
        $row["mag_folio"]=($row["mag_year"]!=date(Y) ? "1" : $row["mag_folio"]+1);
        $row["mag_year"]=($row["mag_year"]!=date(Y) ? date(Y) : $row["mag_year"]);
      }
      
      $this->view->row=$row;
      
    }

    public function newAction()
    {
      $request=$this->getRequest();
      if($request->getParam('mag_folio')==null)
      {
        throw new Zend_Controller_Action_Exception('No puedes accesar esta página de forma directa');
      }
      else
      {
        $costo=0;
        foreach($request->getParam('mag_cotizacion_valor') as $key=>$value)
        {
          $costo+=$value;
        }
        $conn=new Application_Model_Database();
        $sql="
          INSERT into magove_t_folios (
          mag_folio, 
          mag_year, 
          mag_costo, 
          mag_comment, 
          mag_currency, 
          mag_iva, 
          mag_pago, 
          mag_cambio, 
          insert_id, insert_date, active)
          VALUES('".$request->getParam("mag_folio")."','"
                .$request->getParam("mag_year")."','"
                .$costo."','"
                .$request->getParam("mag_comment")."','"
                .$request->getParam("mag_currency")."',"
                .$request->getParam("mag_iva").", '"
                .($request->getParam("mag_pago")!='' ? $this->deFormatDate($request->getParam("mag_pago")) : '')."', '"
                .$request->getParam("mag_cambio")."',
              1,'".date(Y."-".m."-".d)."',1);";
        $rs=$conn->doQuery($sql);
      	$sql="SELECT @@IDENTITY AS 'mag_id';";
        $rs=$conn->doQuery($sql);
        $row=$rs->fetch();
        
        $request=$request->getParam("mag_cotizacion");
        foreach($request->getParam('mag_cotizacion_valor') as $key=>$value)
        {
          $sql="
          INSERT into magove_t_cotizaciones (mag_id, 
          mag_cotizacion_valor, 
          mag_cotizacion, 
          insert_id, insert_date, active)
          VALUES('".$row["mag_id"]."','"
                  .$value."','"
                  .$request[$key]."',
          1,'".date(Y."-".m."-".d)."',1);";
          $rs=$conn->doQuery($sql);
        }
        
        $request=$request->getParam("mag_compra");
        $mail=$request->getParam("mag_mail");
        foreach($request->getParam('mag_compra_valor') as $key=>$value)
        {
          $sql="
    			INSERT into magove_t_compras (mag_id, 
          mag_compra_valor, 
          mag_compra, 
          mag_mail, 
          insert_id, insert_date, active)
    			VALUES('".$mag_id."','"
                  .$value."','"
                  .$request[$key]."','"
                  .$mail[$key]."',
          1,'".date(Y."-".m."-".d)."',1);";
          $rs=$conn->doQuery($sql);
        }
      }
    }


}



